#include <iostream>
#include <vector>
using namespace std;

int main() 
{
    //1、构建n和q
    int n,q;
    cin >> n >> q;

    //输入n个整数
    vector<int> arr(n+1);
    for(int i = 1; i <= n; i++)
        cin >> arr[i];
    
    //预处理前缀和数组
    vector<long long> dp(n+1);
    for(int i = 1; i <= n; i++)
        dp[i] = dp[i - 1] + arr[i];

    //使用前缀和
    int l,r;
    while(q--)
    {
        cin >> l >> r;
        cout << dp[r] - dp[l - 1] << endl;
    }
}

class MedianFinder {
    priority_queue<int> left;
    priority_queue<int, vector<int>, greater<int>> right;
public:
    MedianFinder() {

    }
    
    void addNum(int num) {
        if(left.size() == right.size())
        {
            if(left.empty() || num <= left.top())
            {
                left.push(num);
            }
            else
            {
                right.push(num);
                left.push(right.top());
                right.pop();
            }
        }
        else
        {
            if(num <= left.top())
            {
                left.push(num);
                right.push(left.top());
                left.pop();
            }
            else
            {
                right.push(num);
            }
        }
    }
    
    double findMedian() {
        if(left.size() == right.size())
            return (left.top() + right.top()) / 2.0;
        else
            return left.top();
    }
};

/**
 * Your MedianFinder object will be instantiated and called as such:
 * MedianFinder* obj = new MedianFinder();
 * obj->addNum(num);
 * double param_2 = obj->findMedian();
 */


class Solution {
    typedef pair<int, int> PII;
    int dx[4] = {0,0,-1,1};
    int dy[4] = {1,-1,0,0};
public:
    vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color) 
    {
        int prev = image[sr][sc];
        if(prev == color) return image; //处理边界情况
        int n = image.size(), m = image[0].size();
        queue<PII> q;
        q.push({sr, sc});
        while(q.size())
        {
            auto [a,b] = q.front();
            q.pop();
            image[a][b] = color; // 上色
            for(int i = 0; i < 4; i++)
            {
                int x = a + dx[i], y = b + dy[i];
                if(x >= 0 && x < n && y >= 0 && y < m && image[x][y] == prev)
                {
                    q.push({x, y});
                }
            }
        }
        return image;
    }
};

class Solution {
    int n, m;
    int dx[4] = {0, 0, 1, -1};
    int dy[4] = {1, -1, 0, 0}; 
public: 
    void solve(vector<vector<char>> &board) 
    {
        n = board.size(), m = board[0].size();
        //将与边界区域相关联的'O'处理
        for (int j = 0; j < m; j++) 
        {
            if (board[0][j] == 'O' )
                bfs(board, 0, j);
            if(board[n - 1][j] == 'O')
                bfs(board, n - 1, j);
        }

        for (int i = 0; i < n; i++) 
        {
            if(board[i][0] == 'O' )
                bfs(board, i, 0);
            if(board[i][m - 1] == 'O')
                bfs(board, i, m - 1);
        }

        //将'.'和'O'还原
        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++)
            {
                if(board[i][j] == 'O')
                    board[i][j] = 'X';
                else if(board[i][j] == '.') 
                    board[i][j] = 'O';
            }
    }

    void bfs(vector<vector<char>>& board, int i, int j) 
    {
        queue<pair<int, int>> q;
        q.push({i, j});
        board[i][j] = '.';

        while (q.size()) 
        {
            auto [a, b] = q.front();
            q.pop();
            for (int k = 0; k < 4; k++) 
            {
                int x = dx[k] + a, y = dy[k] + b;
                if (x >= 0 && x < n && y >= 0 && y < m && board[x][y] == 'O')
                {
                    q.push({x, y});
                    board[x][y] = '.';
                }
            }
        }
    }
};

class Solution {
    bool visi[101][101];
    int m,n;
    int dx[4] = {0,0,-1,1};
    int dy[4] = {-1,1,0,0};

public:
    int nearestExit(vector<vector<char>>& maze, vector<int>& entrance) 
    {
        int n = maze.size(), m = maze[0].size();
        queue<pair<int, int>> q;
        q.push({entrance[0],entrance[1]});
        visi[entrance[0]][entrance[1]] = true;

        int step = 0;
        while(q.size())
        {
            step++;
            int sz = q.size();
            for(int i = 0; i < sz; i++)
            {
                auto [a, b] = q.front();
                q.pop();
                for(int k = 0; k < 4; k++)
                {
                    int x = dx[k] + a, y = dy[k] + b;
                    if(x >= 0 && x < n && y >= 0 && y < m && !visi[x][y] && maze[x][y] == '.')
                    {
                        if(x == 0 || x == n - 1 || y == 0 || y == m - 1)
                            return step;
                        q.push({x, y});
                        visi[x][y] = true;
                    }
                }
            }
        }

        return -1;
    }
};

class Solution {
public:
    int minMutation(string startGene, string endGene, vector<string>& bank) 
    {


        unordered_set<string> visi; //用于标记状态
        unordered_set<string> hash(bank.begin(), bank.end()); //用于快速判断修改的基因
        //边界情况处理
        if(startGene == endGene) return 0;
        if(!hash.count(endGene)) return -1;
        string change = "ACGT";
        int ret = 0;
        queue<string> q;

        q.push(startGene);
        visi.insert(startGene);

        while(q.size())
        {
            int sz = q.size();
            ret++;
            while(sz--)
            {
                auto s = q.front();
                q.pop();
                for(int i = 0; i < 8; i++)
                {
                    string tmp = s;
                    for(int j = 0; j < 4; j++)
                    {
                        tmp[i] = change[j];//修改基因
                        if(hash.count(tmp) && !visi.count(tmp))
                        {
                            if(tmp == endGene) return ret;
                            q.push(tmp);
                            visi.insert(tmp);
                        }
                    }
                }
            }
        }
        return -1;
    }
};

class Solution {
    bool visi[51][51];
    int dx[4] = {0,0,1,-1};
    int dy[4] = {-1,1,0,0};
    int m, n;
public:
    int cutOffTree(vector<vector<int>>& forest) 
    {
        // 1.处理待砍树
        n = forest.size(), m = forest[0].size();
        vector<pair<int, int>> trees;
        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++)
                if(forest[i][j] > 1) trees.push_back({i,j});

        sort(trees.begin(), trees.end(), [&](const pair<int, int> p1, const pair<int, int> p2)
        {
            return forest[p1.first][p1.second] < forest[p2.first][p2.second];

        });

        //2.按顺序砍树

        int bx = 0, by = 0;// 起始下标
        int ret = 0; 
        for(auto & [a, b] : trees)
        {
            int step = bfs(forest, bx, by, a, b);
            if(step == -1) return -1;
            ret += step;
            bx = a;
            by = b;
        }
        return ret;
    }

    int bfs(vector<vector<int>>& forest,int bx, int by, int ex, int ey)
    {
        //处理边界情况
        if(ex == bx && ey == by) return 0;

        queue<pair<int, int>> q;
        q.push({bx, by});
        memset(visi,0, sizeof(visi));
        visi[bx][by] = true;

        int ret = 0;
        while(q.size())
        {
            int sz = q.size();
            ret++;
            while(sz--)
            {
                auto [a, b] = q.front();
                q.pop();
                for(int k = 0; k < 4; k++)
                {
                    int x = dx[k] + a, y = dy[k] + b;
                    if(x >= 0 && x < n && y >= 0 && y < m && !visi[x][y] && forest[x][y])
                    {
                        if(x == ex && y == ey)
                            return ret;
                        q.push({x, y});
                        visi[x][y] = true;
                    }
                }
            }
        }

        return -1;
    }
};

class Solution {
    int dx[4] = {0,0,-1,1};
    int dy[4] = {-1,1,0,0};
public:
    int numEnclaves(vector<vector<int>>& grid) 
    {
        //将边界上的1 入队列进行bfs
        int m = grid[0].size(), n = grid.size();       
        queue<pair<int, int>> q;
        vector<vector<bool>> visi(n, vector<bool>(m));
        for(int i = 0; i < m; i++)
        {
            if(grid[0][i] == 1) 
            {
                q.push({0,i});
                visi[0][i] = true;
            }
            if(grid[n-1][i] == 1) 
            {
                q.push({n - 1, i});
                visi[n-1][i] = true;
            }
        }
        for(int i = 0; i < n; i++)
        {
            if(grid[i][0] == 1) 
            {
                q.push({i,0});
                visi[i][0] = true;
            }
            if(grid[i][m - 1] == 1) 
            {
                q.push({i, m - 1});
                visi[i][m - 1] = true;
            }
        }

        while(q.size())
        {
            auto [a, b] = q.front();
            q.pop();
            for(int i = 0; i < 4; i++)
            {
                int x = dx[i] + a, y = dy[i] + b;
                if(x >= 0 && x < n && y >= 0 && y < m && !visi[x][y] && grid[x][y] == 1)
                {
                    q.push({x, y});
                    visi[x][y] = true;
                }
            }
        }

        int ret = 0;

        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++)
                if(grid[i][j] == 1 && !visi[i][j]) ret++;
        
        return ret;
    }
};

class Solution {
    int dx[4] = {0,0,-1,1};
    int dy[4] = {-1,1,0,0};
public:
    int maxDistance(vector<vector<int>>& grid) 
    {
        int n = grid.size(), m = grid[0].size();
        vector<vector<int>> dist(n, vector<int>(m, -1));
        queue<pair<int,int>> q;

        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++)
                if(grid[i][j] == 1)
                {
                    dist[i][j] = 0;
                    q.push({i, j});
                }

        while(q.size())
        {
            auto [a, b] = q.front();
            q.pop();
            for(int i = 0; i < 4; i++)
            {
                int x = dx[i] + a, y = dy[i] + b;
                if(x >= 0 && x < n && y >= 0 && y < m && dist[x][y] == -1)
                {
                    q.push({x,y});
                    dist[x][y] = dist[a][b] + 1;
                }
            }
        }
        
        int max = INT_MIN;
        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++)
                if(max < dist[i][j]) max = dist[i][j];
        if(max == 0)    return -1;

        return max;
    }       
};

class Solution {
public:
    bool canFinish(int n, vector<vector<int>>& prerequisites)
    {
        //使用哈希表模拟邻接表
        unordered_map<int, vector<int>> edges;
        vector<int> in(n); // 统计入度

        //创建图
        for(auto &e : prerequisites)
        {
            int a = e[0], b = e[1];
            edges[b].push_back(a);
            in[a]++;
        }

        //拓扑排序
        //将度为0的节点入队列

        queue<int> q;
        for(int i = 0; i < n; i++)
        {
            if(in[i] == 0)
                q.push(i);
        }

        //bfs
        while(q.size())
        {
            int f = q.front();
            q.pop();
            for(int x : edges[f])
            {
                in[x]--;
                if(in[x] == 0)
                    q.push(x);
            }
        }

        for(int a : in)
            if(a != 0) return false;

        return true;
    }
};

class Solution {
    unordered_map<char, unordered_set<char>> edges;
    unordered_map<char, int> in;
    bool cheak;
public:
    string alienOrder(vector<string>& words) 
    {
        //初始化哈希表
        for(auto &e : words)
            for(auto ch : e)
            {
                in[ch] = 0;
            }

        int sz = words.size();
        for(int i = 0; i < sz; i++)
        {
            for(int j = i + 1; j < sz; j++)
            {
                add(words[i], words[j]);
                if(cheak) return "";
            }
        }

        queue<char> q;
        for(auto& [a, b]: in)
        {
            if(b == 0)
                q.push(a);
        }

        string ret = "";
        while(q.size())
        {
            char f = q.front();
            q.pop();
            ret += f;

            for(char ch : edges[f])
            {
                if(--in[ch] == 0) q.push(ch);
            }
        }

        for(auto& [a, b]: in)
        {
            if(b != 0) return "";
        }

        return ret;
    }

    void add(string& s1, string& s2)
    {
        int n = min(s1.size(), s2.size());
        int i = 0;
        for(;i < n; i++)
        {
            if(s1[i] != s2[i])
            {
                char a = s1[i], b = s2[i];
                if(!edges.count(a) || !edges[a].count(b)) // a->b
                {
                    edges[a].insert(b);
                    in[b]++;
                }
                break;
            }
        }
        if(s2.size() == i && i < s1.size()) cheak = true;
    }
};
